#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
检查学生扩展数据
"""
import sys
sys.path.append('.')

from backend.infrastructure.database import db
from backend.models.student import Student
from backend.models.student_extension import StudentExtension
from app import create_app

def check_student_extensions():
    app = create_app()
    with app.app_context():
        try:
            # 查找测试学生
            student = Student.query.filter_by(education_id='1010002').first()
            if not student:
                print("测试学生不存在")
                return False
            
            print(f"找到学生: ID={student.id}, 学籍号={student.education_id}, 姓名={student.name}")
            
            # 查找学生扩展信息
            extensions = StudentExtension.query.filter_by(student_id=student.id).all()
            print(f"学生扩展记录数量: {len(extensions)}")
            
            for ext in extensions:
                print(f"  - 数据年份: {ext.data_year}, 年级: {ext.grade}, 年龄: {ext.age}")
            
            if not extensions:
                print("没有学生扩展数据，创建一条测试数据...")
                new_ext = StudentExtension(
                    student_id=student.id,
                    data_year='2024',
                    grade='一年级',
                    age=7
                )
                db.session.add(new_ext)
                db.session.commit()
                print("✅ 创建学生扩展数据成功")
                return True
            else:
                print("✅ 学生扩展数据存在")
                return True
                
        except Exception as e:
            print(f"检查失败: {e}")
            return False

if __name__ == '__main__':
    success = check_student_extensions()
    if success:
        print("\n✅ 学生扩展数据检查完成")
    else:
        print("\n❌ 学生扩展数据检查失败")
